## Untitled - By: STM32 - 周四 四月 19 2018
import sensor,image,time,lcd,math
import traffic_code,qr_code,figure_code
from pyb import UART,Timer,LED

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.set_auto_whitebal(False)      #关闭白平衡
sensor.set_auto_gain(False)          #关闭自动增益

sensor.skip_frames(time=2000)


lcd.init()                           #Initialize the lcd screen.
uart = UART(3,115200,8,None,1)       #创建串口对象
uart.init(baudrate=115200, bits=8, parity=None, stop=1, timeout=10, flow=0, timeout_char=0, read_buf_len=64)


LED_Red = LED(1)
LED_Green = LED(2)
LED_Blue = LED(3)

TRAFFIC_FLAG = 1   #交通灯识别标志
QR_FLAG = 2        #二维码识别标志
FIGURE_FLAG = 3    #图形识别标志
TRAFFIC_THRESHOLD = 4 #交通灯阈值学习
RES_FLAG = 0       #默认标志

mode_flag = 0
dis_flag = 0


"""-----------------------通讯协议说明-------------------------"""
#启动交通灯识别 [0x55,0x02,0x91,0x01,0x00,0x00,0x00,0xbb]
#交通灯自动学习阈值 [0x55,0x02,0x91,0xff,0x00,0x00,0x00,0xbb]
#交通的识别图形 [0x55,0x02,0x91,0x12,0x00,0x00,0x00,0xbb]
#二维码识别 [0x55,0x02,0x92,0x01,0x00,0x00,0x00,0xbb]
#启动调试模式 [0x55,0x02,0x92,0x02,0x00,0x00,0x00,0xbb]
#关闭调试模式 [0x55,0x02,0x92,0x03,0x00,0x00,0x00,0xbb]


#串口发送函数
def USART_Send(src,length):
    for i in range(length):
        uart.writechar(src[i])

#设置识别模式
def Set_Mode_Flag(flag):
    global mode_flag,dis_flag
    if((flag == RES_FLAG) and (dis_flag == 0)):
        mode_flag = flag
    elif(flag != RES_FLAG):
        mode_flag = flag

#获取模式标志
def Get_Mode_Flag():
    global mode_flag
    return mode_flag


#检测串口数据
def Check_Data():
    global dis_flag,uart
    #print(uart.any())
    if(uart.any()):
        #if(uart.readchar() == 0x55):
        #time.sleep(500)
        data = uart.readline()
        #data = uart.read(8)
        print(data)

        if(len(data) >= 8):
            #print(data)
            if((data[0] == 0x55)&(data[1] == 0x02)&(data[7] == 0xBB)):  #数据完整性判断
                if(data[2] == 0x91):
                    if(data[3] == 0x01):
                        Set_Mode_Flag(TRAFFIC_FLAG)  #启动交通灯识别
                    elif(data[3] == 0xff):
                        Set_Mode_Flag(TRAFFIC_THRESHOLD)#交通灯自动学习阈值
                    else:
                        Set_Mode_Flag(FIGURE_FLAG)   #启动颜色识别
                elif(data[2] == 0x92):
                    if(data[3] == 0x01):
                        Set_Mode_Flag(QR_FLAG)       #二维码识别
                    elif(data[3] == 0x02):           #启动调试模式
                        dis_flag = 1
                    elif(data[3] == 0x03):           #关闭调试模式
                        dis_flag = 0
                else:
                     Set_Mode_FLAG(RES_FLAG)


#Set_Mode_Flag(TRAFFIC_FLAG)   #启动颜色识别
#dis_flag = 1
back_data = [0x55,0x02,0x92,0x00,0x00,0x00,0x00,0xbb]
while(True):
    Check_Data()   #检测接收到的串口数据
    #USART_Send(back_data, 8)
    if(Get_Mode_Flag() == TRAFFIC_FLAG):        #交通灯识别
        LED_Red.on()
        LED_Green.off()
        LED_Blue.off()
        sensor.skip_frames(time=200)
        for i in range(10):
            back_data[4] = traffic_code.Color_Check()
            print(back_data[4])
            if( back_data[4] != 0):
                if(i >= 5):
                    back_data[2] = 0x91
                    back_data[3] = 0x01
                    USART_Send(back_data, 8)
                    print(back_data[4])
                    Set_Mode_Flag(RES_FLAG)
                    break
    elif(Get_Mode_Flag() == TRAFFIC_THRESHOLD):  #自动学习交通灯阈值
        LED_Red.on()
        LED_Green.off()
        LED_Blue.on()
        traffic_code.Auto_Threshold()
        Set_Mode_Flag(TRAFFIC_FLAG)

    elif(Get_Mode_Flag() == QR_FLAG):            #二维码识别
        LED_Red.off()
        LED_Green.on()
        LED_Blue.off()
        if(qr_code.Color_Check(uart)):
            print("qr_yes")
            Set_Mode_Flag(RES_FLAG)

    elif(Get_Mode_Flag() == FIGURE_FLAG):    #图形识别
        LED_Red.off()
        LED_Green.off()
        LED_Blue.on()
        rgb_value = figure_code.Color_Check()
        if((rgb_value[0]!=0) or (rgb_value[1]!=0) or (rgb_value[2]!=0)):
            back_data[2] = 0x91
            back_data[3] = 0x02
            back_data[4] = rgb_value[0]
            back_data[5] = rgb_value[1]
            back_data[6] = rgb_value[2]
            USART_Send(back_data, 8)
            print(rgb_value)
            Set_Mode_Flag(RES_FLAG)
    else:
        LED_Green.on()
        LED_Red.on()
        LED_Blue.on()
        sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
        sensor.set_windowing((128,180))
        img = sensor.snapshot()
        lcd.display(img)








